#include "Stdafx.h"
#include "Object2D.h"

namespace Pulpy
{
	bool Object2D::Contain(Object2D &ob) 
	{
		if(&ob == this)
		{
			Logger::getInstance().addLog("Object2D::Error checking Contain on self");
			return false;
		}
		if(!ob._Solid || !_Solid)
			return false;
		if(ob.getWidth() > _Width || ob.getHeight() > _Height)
			return false;
		//Circle & Circle
		if(ob._Radius > 0 && _Radius > 0 && (ob._Position - _Position).getMag() == 0 && _Radius >= ob._Radius)
			return true;
		//If containing target is a square. Treat container as square. Take width
		return (_Position.getX() - _Width / 2) >= (ob._Position.getX() - ob._Width / 2) && 
			(_Position.getX() + _Width / 2) <= (ob._Position.getX() + ob._Width / 2) &&
			 (_Position.getY() - _Height/ 2) >= (ob._Position.getY() - ob._Height/ 2) && 
			(_Position.getY() + _Height/ 2) <= (ob._Position.getY() + ob._Height/ 2);
		
		
	}
	bool Object2D::Contain(Vector2D<float> &v)
	{
		if(v.getX() <= _Position.getX() + _Width / 2
			&& v.getX() >= _Position.getX() - _Width / 2
			&& v.getY() >= _Position.getY() - _Height / 2
			&& v.getY() <=  _Position.getY() + _Height / 2)
			return true;
		return false;
	}
	bool Object2D::Intersects(Object2D &ob)
	{

		if(&ob == this)
		{
			Logger::getInstance().addLog("Object2D::Error checking Intersects on self");
			return false;
		}
		if(Contain(ob))
			return true;
				//Circle to circle intersect
		if(ob._Radius > 0 && _Radius > 0 &&  (ob._Position - _Position).getMag()- _Radius + ob._Radius <= 0)
			return true;
		return
				!((_Position.getX() - _Width / 2) >(ob._Position.getX() + ob._Width/ 2) ||
	   (_Position.getX() + _Width/ 2) < (ob._Position.getX() - ob._Width/ 2) ||
       (_Position.getY() - _Height/ 2) > (ob._Position.getY() + ob._Height/ 2) ||
	   (_Position.getY() + _Height/ 2) <(ob._Position.getY() - ob._Height/ 2));
		return false;
	}
	void Object2D::drawPlane(GLuint texture)
	{
		glPushMatrix();
		if(texture != 0)
		{
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			//glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
			glDisable(GL_DEPTH_TEST);
			glEnable(GL_TEXTURE_2D);
			glEnable(GL_BLEND);
			glBindTexture(GL_TEXTURE_2D, texture);
		}
		glTranslatef(_Position.getX(), _Position.getY(), 0);
		glBegin(GL_QUADS);
		if (AnimationInvert == false)
		{
			glTexCoord2f(0,1); glVertex2f(_Width/2, -_Height/2);
			glTexCoord2f(1,1); glVertex2f(-_Width/2, -_Height/2);
			glTexCoord2f(1,0); glVertex2f(-_Width/2, _Height/2);
			glTexCoord2f(0,0); glVertex2f(_Width/2, _Height/2);			
		}
		else
		{
			glTexCoord2f(1,1); glVertex2f(_Width/2, -_Height/2);
			glTexCoord2f(0,1); glVertex2f(-_Width/2,- _Height/2);
			glTexCoord2f(0,0); glVertex2f(-_Width/2, _Height/2);
			glTexCoord2f(1,0); glVertex2f(_Width/2, _Height/2);
		}
		glEnd();
		if(texture != 0)
		{
			glDisable(GL_TEXTURE_2D);
			glDisable(GL_BLEND);
			glEnable(GL_DEPTH_TEST);
		}
		glPopMatrix();
	}

	void Object2D::drawPlane(Sprite * sprite, int index)
	{
		glPushMatrix();
		if(sprite != NULL)
		{
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			glDisable(GL_DEPTH_TEST);
			glEnable(GL_TEXTURE_2D);
			glEnable(GL_BLEND);
		}
			sprite->setIndex(index);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			glDisable(GL_DEPTH_TEST);
			glBindTexture(GL_TEXTURE_2D, sprite->getTexID());
			glTranslatef(_Position.getX(), _Position.getY(), 0);			
			glBegin(GL_QUADS);
			if (AnimationInvert == false)
			{
				glTexCoord2f(sprite->getMinX(),sprite->getMinY()); glVertex2f(_Width/2, _Height/2);
				glTexCoord2f(sprite->getMaxX(),sprite->getMinY()); glVertex2f(-_Width/2, _Height/2);
				glTexCoord2f(sprite->getMaxX(),sprite->getMaxY()); glVertex2f(-_Width/2, -_Height/2);
				glTexCoord2f(sprite->getMinX(),sprite->getMaxY()); glVertex2f(_Width/2, -_Height/2);
			}
			else
			{
				glTexCoord2f(sprite->getMaxX(),sprite->getMinY()); glVertex2f(_Width/2, _Height/2);
				glTexCoord2f(sprite->getMinX(),sprite->getMinY()); glVertex2f(-_Width/2, _Height/2);
				glTexCoord2f(sprite->getMinX(),sprite->getMaxY()); glVertex2f(-_Width/2, -_Height/2);
				glTexCoord2f(sprite->getMaxX(),sprite->getMaxY()); glVertex2f(_Width/2, -_Height/2);
				
			}
			glEnd();
			if(sprite != NULL)
			{
				glDisable(GL_BLEND);
				glEnable(GL_DEPTH_TEST);
				glDisable(GL_TEXTURE_2D);
			}
			glPopMatrix();
	}
	void Object2D::drawCircle()
	{
		double y1=_Position.getX()+_Radius;
		double x1= _Position.getY();
		glBegin(GL_TRIANGLE_FAN);
	    for(double angle=0.0f;angle<=(2.0f*3.14159);angle+=0.01f)
	    {
			double x2=_Position.getX()+(_Radius*(float)sin((double)angle));
			double y2=_Position.getY()+(_Radius*(float)cos((double)angle));
			glVertex2d(x1,y1);
			y1=y2;
			x1=x2;
	    }
		glEnd();
	}
}